package com.itheima.api.sms;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.sms.SmsCouponMapper;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsCoupon;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements SmsCouponService {

    @Autowired
    private SmsCouponMapper smsCouponMapper;

    @Override
    public void update(SmsCoupon smsCoupon) {
        smsCouponMapper.update(smsCoupon);
    }

    //优惠券的分页查询
    @Override
    public TableDataInfo page(Integer pageNum, Integer pageSize, Integer type, String name) {
        Page<SmsCoupon> pageInfo = new Page<>(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),SmsCoupon::getName,name);
        queryWrapper.eq(type!=null,SmsCoupon::getType,type);

        smsCouponMapper.selectPage(pageInfo,queryWrapper);

        tableDataInfo.setList(pageInfo.getRecords());
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotal(pageInfo.getTotal());
        tableDataInfo.setTotalPage(pageInfo.getPages());

        return tableDataInfo;
    }


}
